package cn.school.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;

@WebFilter(value = "/*", filterName = "encoding") // 拦截所有的请求
public class EncodingFilter implements Filter {

	@Override
	public void destroy() {

	}

	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
			throws IOException, ServletException {
		System.out.println("EncodingFilter 进入了doFilter");
		HttpServletRequest req = (HttpServletRequest) arg0;// 强转
		System.out.println("当前访问路径：" + req.getServletPath());
		req.setCharacterEncoding("utf-8");

		arg2.doFilter(req, arg1);// 放行！！！

		System.out.println("EncodingFilter 离开了doFilter");
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		System.out.println("filter init ");
	}

}
